package com.yylending.las.lending.process.chooseamount;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by fan on 2017/6/28.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class ChooseAmountPresenter extends Presenter<ChooseAmountModel, ChooseAmountPresenter.View> {

	public static class Generator implements PresenterFactory<ChooseAmountPresenter> {
		@Override
		public ChooseAmountPresenter create() {
			return new ChooseAmountPresenter();
		}
	}

	protected interface View extends IView {
		void getRateSuccess(ChooseAmountModel.Rate rate);

		void getRateFailure(String tips);
	}

	private View mView;

	@Override
	public void attach(ChooseAmountPresenter.View view) {
		mView = view;
		mModel = new ChooseAmountModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	void getRate() {
		execute(null, FUNCTIONAL_CODE, true);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getRateResult(ChooseAmountModel.GetRateResult result) {
		if (BaseResultEntity.isResponseSuccess(result)) {
			if (null != result.getData()) {
				mView.getRateSuccess(result.getData());
			}
		} else {
			mView.getRateFailure(result.getRetMsg());
		}
	}
}
